﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Models;
using MensUnoShop.Areas.Admin.Filters;
using MvcPaging;
using MensUnoShop.Filters;

namespace MensUnoShop.Areas.Admin.Controllers
{
    [ManagerCheckFilter]
    [NoCacheFilter]
    public class SalesKitController : Controller
    {
        //
        // GET: /Admin/SalesKit/
        private int defaultPageSize = 30;

        #region 生日折扣
        public ActionResult Birthday()
        {
            DBSaleskit dbs = new DBSaleskit();
            //var now =  dbs.GetBirthdayDiscount();
            //ViewData["now"] = now;
            return View();
        }

        [HttpPost]
        public ActionResult Birthday(int percent)
        {
            DBSaleskit dbs = new DBSaleskit();
            //dbs.SetNewBirthdayDiscount(percent);

            return RedirectToAction("Birthday");
        }
        #endregion

        #region 優惠券
        public ActionResult Coupon()
        {
            DBSaleskit dbs = new DBSaleskit();
            List<V_CouponList> sk = dbs.GetCouponDiscount();
            ViewData["couponlist"] = sk;
            return View();
        }

        [HttpPost]
        public ActionResult Coupon(string title, int amount, int price)
        {
            DBSaleskit dbs = new DBSaleskit();
            string[] rex = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X","Y","Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7","8","9" };
            string coupon = string.Empty;
            for (int i = 1; i <= amount; i++)
            {
                while (true)
                {
                    for (int j = 0; j < 10; j++)
                    { 
                        Random r = new Random(Guid.NewGuid().GetHashCode());
                        coupon += rex[r.Next(rex.Length - 1)];
                    }
                    if (dbs.SetNewCoupon(title, coupon, price))
                        break;
                }
                coupon = string.Empty;
            }

            return RedirectToAction("Coupon");
        }
        #endregion


        #region 買千折百
        public ActionResult Handsel(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;

            List<USalesKit> sk = SalesKitFactory.GetManagerHanselList();
            ViewData["hansellist"] = sk.ToPagedList(currentPageIndex, defaultPageSize);
            return View();
        }

        [HttpPost]
        public ActionResult Handsel(FormCollection fm)
        {
            if (fm["Act"] == "ChgStatus")
                SalesKitFactory.UptManagerHanselStatus(fm["ssguid"]);
            else if (fm["Act"] == "Del")
            {
                SalesKitFactory.DelManagerHansel(fm["ssguid"]);
                TempData["alert"] = "標籤已刪除";
            }
            return RedirectToAction("Handsel");
        }

        
        public ActionResult HandselSetup(string id)
        {
            string titlestring = "{0}買千折百活動";
            string actstring = string.Empty;
            USalesKit sk = null;
            if (id == "0")
            {
                titlestring = string.Format(titlestring, "新增");
                actstring = "Add";
            }
            else
            {
                titlestring = string.Format(titlestring, "更新");
                actstring = "Upt";
                sk = SalesKitFactory.GetManagerHansel(id);
            }

            ViewData["title"] = titlestring;
            ViewData["Act"] = actstring;
            ViewData["saleskit"] = sk;
            return View();
        }

        [HttpPost]
        public ActionResult HandselSetup(string id, string act, string stitle, string startdate, string enddate)
        {
            string msg = string.Empty;
            if (act == "Add")
                msg = SalesKitFactory.AddManagerHansel(stitle, startdate, enddate, ref id);
            else if (act == "Upt")
                msg = SalesKitFactory.SetManagerHansel(id, stitle, startdate, enddate);

            TempData["alert"] = msg;
            return RedirectToAction("Handsel");
        }

        public JsonResult api_handsel(string id)
        { 
            List<USalesKitHandsel> handsellist;
            SalesKitFactory.GetManagerHanselList(id, out handsellist);

            return Json(new { code = "200", shid=handsellist.Select(h=>h.SHid), limit = handsellist.Select(h=>h.SHbottomlimit),giftpoint=handsellist.Select(h=>h.SHgiftprice)}, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult api_handsel(string id, string act, string shid, string limit, string giftpoint)
        {
            List<USalesKitHandsel> handsellist;
            string msg = string.Empty;

            if (act == "Add")
                SalesKitFactory.AddManagerHanselDetail(id, limit, giftpoint, ref msg);
            else if(act =="Del")
                SalesKitFactory.DelManagerHanselDetail(shid, ref msg);
            
            
            SalesKitFactory.GetManagerHanselList(id, out handsellist);

            if(msg == string.Empty)
                return Json(new { code = "200", shid = handsellist.Select(h => h.SHid), limit = handsellist.Select(h => h.SHbottomlimit), giftpoint = handsellist.Select(h => h.SHgiftprice) });
            else
                return Json(new { code = "500",response = msg});
        }
        #endregion

        #region 合購
        public ActionResult Combo(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            List<USalesKit> sk = SalesKitFactory.GetManagerComboList();
            ViewData["combolist"] = sk.ToPagedList(currentPageIndex, defaultPageSize);
            return View();
        }

        [HttpPost]
        public ActionResult Combo(FormCollection fm)
        {
            if (fm["Act"] == "ChgStatus")
                SalesKitFactory.UptManagerComboStatus(fm["ssguid"]);
            else if (fm["Act"] == "Del")
            {
                SalesKitFactory.DelManagerCombo(fm["ssguid"]);
                TempData["alert"] = "標籤已刪除";
            }
            return RedirectToAction("Combo");
        }


        public ActionResult ComboSetup(string id)
        {
            string titlestring = "{0}合購活動";
            string actstring = string.Empty;
            USalesKit sk = null;
            if (id == "0")
            {
                titlestring = string.Format(titlestring, "新增");
                actstring = "Add";
            }
            else
            {
                titlestring = string.Format(titlestring, "更新");
                actstring = "Upt";
                sk = SalesKitFactory.GetManagerCombo(id);
            }

            ViewData["title"] = titlestring;
            ViewData["Act"] = actstring;
            ViewData["saleskit"] = sk;
            return View();
        }

        [HttpPost]
        public ActionResult ComboSetup(string id, string act, string stitle, string tagid, int pices, int comboprice, string startdate, string enddate)
        {
            string msg = string.Empty;
            if (stitle != string.Empty)
            {
                if (act == "Add")
                {
                    //1. 建活動標籤
                    tagid = TagFactory.CheckTag(stitle, Config.TagType.COMBO).ToString();
                    msg = SalesKitFactory.AddManagerCombo(stitle, tagid, pices, comboprice, startdate, enddate, ref id);
                }
                else if (act == "Upt")
                {
                    //修改標題
                    if (TagFactory.UptManagerTag(tagid, stitle))
                        msg = SalesKitFactory.SetManagerCombo(id, stitle, tagid, pices, comboprice, startdate, enddate);
                    else
                        msg = "活動更新失敗!!(500)";
                }
            }
            else
                msg = "請輸入活動名稱";

            TempData["alert"] = msg;
            return RedirectToAction("Combo");
        }
        /*
        public JsonResult api_combo(string id)
        {
            List<USalesKitHandsel> handsellist;
            SalesKitFactory.GetManagerHanselList(id, out handsellist);

            return Json(new { code = "200", shid = handsellist.Select(h => h.SHid), limit = handsellist.Select(h => h.SHbottomlimit), giftpoint = handsellist.Select(h => h.SHgiftprice) }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult api_combo(string id, string act, string shid, string limit, string giftpoint)
        {
            List<USalesKitHandsel> handsellist;
            string msg = string.Empty;

            if (act == "Add")
                SalesKitFactory.AddManagerHanselDetail(id, limit, giftpoint, ref msg);
            else if (act == "Del")
                SalesKitFactory.DelManagerHanselDetail(shid, ref msg);


            SalesKitFactory.GetManagerHanselList(id, out handsellist);

            if (msg == string.Empty)
                return Json(new { code = "200", shid = handsellist.Select(h => h.SHid), limit = handsellist.Select(h => h.SHbottomlimit), giftpoint = handsellist.Select(h => h.SHgiftprice) });
            else
                return Json(new { code = "500", response = msg });
        }*/
        #endregion
    }
}
